package com.nowcoder.community.service;

import com.nowcoder.community.dao.UserMapper;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.MailClient;
import com.nowcoder.community.util.RedisKeyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ForgetPasswordService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private MailClient mailClient;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private UserMapper userMapper;

    public String sendForgetPasswordCode(String email) {
        // 生成拥有者、验证码
        String emailAndCodeOwner = CommunityUtil.generateUUID();
        String code = CommunityUtil.generateUUID().substring(0, 4);

        // 将邮箱和验证码存入redis，有效时间为5分钟
        String forgetPasswordEmailKey = RedisKeyUtil.getForgetPasswordEmailKey(emailAndCodeOwner);
        String forgetPasswordCodeKey = RedisKeyUtil.getForgetPasswordCodeKey(emailAndCodeOwner);
        redisTemplate.opsForValue().set(forgetPasswordEmailKey, email, 5, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(forgetPasswordCodeKey, code, 5, TimeUnit.MINUTES);

        // 发送验证码到邮箱
        Context context = new Context();
        context.setVariable("email", email);
        context.setVariable("code", code);
        String content = templateEngine.process("/mail/forget", context);
        mailClient.sendMail(email, "nowcoder community 找回密码", content);

        // 将拥有者返回给controller，用于给客户端发送cookie
        return emailAndCodeOwner;
    }

    public Map<String, Object> resetPassword(String email, String verifyCode, String newPassword, String emailAndCodeOwner) {
        Map<String, Object> map = new HashMap<>();
        //判断是否为redis中临时存储的邮箱
        String forgetPasswordEmailKey = RedisKeyUtil.getForgetPasswordEmailKey(emailAndCodeOwner);
        String emailFromRedis = (String) redisTemplate.opsForValue().get(forgetPasswordEmailKey);
        if (StringUtils.isBlank(email) || StringUtils.isBlank(emailFromRedis) || !email.equals(emailFromRedis)) {
            map.put("emailMsg", "邮箱错误！");
            return map;
        }

        // 判断验证码是否正确
        String forgetPasswordCodeKey = RedisKeyUtil.getForgetPasswordCodeKey(emailAndCodeOwner);
        String codeFromRedis = (String) redisTemplate.opsForValue().get(forgetPasswordCodeKey);
        if (StringUtils.isBlank(verifyCode) || StringUtils.isBlank(codeFromRedis) || !verifyCode.equals(codeFromRedis)) {
            map.put("codeMsg", "验证码错误！");
            return map;
        }

        // 判断新密码是否为空
        if (StringUtils.isBlank(newPassword)) {
            map.put("passwordMsg", "密码不能为空！");
            return map;
        }

        // 重置密码
        User user = userMapper.selectUserByEmail(email);
        userMapper.updatePassword(user.getId(), CommunityUtil.md5(newPassword + user.getSalt()));

        return map;
    }

}
